//
//  ChatInfoViewController.m
//  YMIM
//
//  Created by admin on 2022/3/25.
//

#import "ChatInfoViewController.h"
#import <Masonry/Masonry.h>
#import "NickNameViewController.h"
#import <SDWebImage/SDWebImage.h>
#import <MJExtension/MJExtension.h>
#import "UIViewController+HUD.h"

@interface ChatInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIImageView *headImageView;
@property (nonatomic,strong)UILabel *nameLable;
@property (nonatomic,strong)UIView *headerView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UILabel *nickNameLable;
@property (nonatomic,strong)UIView *footerView;
@property (nonatomic,strong)UIButton *deleteButton;
@property (nonatomic,strong)UISwitch *distubSwithBtn;
@property (nonatomic,strong)UISwitch *skiTopSwithBtn;
@property (nonatomic,strong)NSString *nickName;
@property (nonatomic, assign) NSInteger isTop;
@property (nonatomic, assign) NSInteger isDisturb;
@end

@implementation ChatInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"好友详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initview];
    [self loadData];
    [self loadTableData];
    // Do any additional setup after loading the view.
}
#pragma mark 个人详情
-(void)loadData{
    
    [[YMIMClient sharedClient].friendManager getFriendDetailWithFriendId:self.tid completion:^(YMFriendInfo * _Nonnull info, YMErrorCode code) {
       
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:info.friendAvatar]];
        self.nameLable.text = info.friendName;
        if (info.noteName == nil) {
            
            self.nickName = @"暂无备注";
        }else{
            
            self.nickName = info.noteName;
        }
        
        NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:0];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
    
    
}
#pragma mark 获取置顶和免打扰状态
-(void)loadTableData{
    
    [[YMIMClient sharedClient].chatManager getConversationWithTargetId:self.targetId completion:^(YMConversation * _Nonnull conversation, YMErrorCode code) {
     
        YMGroupInfo *groupInfo = [conversation valueForKey:@"groupInfo"];
        self.isTop = groupInfo.isTop;
        self.isDisturb = groupInfo.isDisturb;
        NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        
        
    }];
    
}
-(void)initview{
    
    [self.view addSubview:self.tableView];
    [self.headerView addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.headerView.mas_top).offset(15);
        make.left.mas_equalTo(self.headerView.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
        
    }];
    
    [self.headerView addSubview:self.nameLable];
    [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.headImageView.mas_centerX);
        make.top.mas_equalTo(self.headImageView.mas_bottom).offset(5);
        make.width.mas_equalTo(80);
        
    }];

    
    [self.footerView addSubview:self.deleteButton];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.footerView.mas_centerY);
        make.centerX.mas_equalTo(self.footerView.mas_centerX);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width-100);
        make.height.mas_equalTo(45);
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==1) {
        
        return 2;
    }else{
        
        return 1;
    }

 
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if (indexPath.section==0) {
        
        if (indexPath.row==0) {
            
            cell.textLabel.text = @"备注：";
            self.nickNameLable.text = self.nickName;
            [cell.contentView addSubview:self.nickNameLable];
            [self.nickNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.right.mas_equalTo(cell.contentView.mas_right).offset(-15);
                make.centerY.mas_equalTo(cell.contentView.mas_centerY);
                make.width.mas_equalTo(160);
            }];
        
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
    }else if (indexPath.section==1){
        
        
        if (indexPath.row==0) {
            
            cell.textLabel.text = @"消息免打扰";
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:self.distubSwithBtn];
            self.distubSwithBtn.on = self.isDisturb;
            [self.distubSwithBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.right.mas_equalTo(cell.contentView.mas_right).offset(-15);
                make.centerY.mas_equalTo(cell.contentView.mas_centerY);
            }];
            
        }else if (indexPath.row==1){
            
            cell.textLabel.text = @"置顶聊天";
            cell.textLabel.font =[UIFont systemFontOfSize:15];;
            [cell.contentView addSubview:self.skiTopSwithBtn];
           
            self.skiTopSwithBtn.on = self.isTop;
            [self.skiTopSwithBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.right.mas_equalTo(cell.contentView.mas_right).offset(-15);
                make.centerY.mas_equalTo(cell.contentView.mas_centerY);
            }];
            
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        
    }else if (indexPath.section==2){
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"清空聊天记录";
        cell.textLabel.font =[UIFont systemFontOfSize:15];;
        
    }
    
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
        
        NickNameViewController *vc = [[NickNameViewController alloc]init];
        vc.tid = self.tid;
        [vc setModifyNickName:^(NSString * _Nonnull name) {
           
            [self loadData];
            
        }];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.section==2){
        
        [self clearHistory];
    }
  
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   return 55;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}
#pragma mark 删除好友
-(void)deleteAction{
    
    NSString *message = [NSString stringWithFormat:@"确定要删除好友%@ ？",self.nameLable.text];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"删除好友" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
        [[YMIMClient sharedClient].friendManager deleteFriendWithFriendId:self.tid completion:^(YMErrorCode code) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"DeleteFriend" object:nil];
            [self showHint:@"删除好友成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:confirm];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}
//消息免打扰
-(void)disturbAction{
    
    [[YMIMClient sharedClient].chatManager doNotDisturbConversationWithTargetId:self.targetId completion:^(YMErrorCode code) {
       
        [self showHint:@"设置成功"];
     
    }];
    
}
//置顶会话
-(void)skipOnAction{
    
    [[YMIMClient sharedClient].chatManager topConversationWithTargetId:self.targetId completion:^(YMErrorCode code) {
       
        [self showHint:@"设置成功"];

    }];
    
}
//清空聊天记录
-(void)clearHistory{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"清空聊天记录" message:@"确定要清空聊天记录 ？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
        [[YMIMClient sharedClient].chatManager clearMessageWithTargetId:self.targetId completion:^(YMErrorCode code) {
           
            [self showHint:@"清空聊天记录成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }];
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:confirm];
    [self presentViewController:alertVC animated:YES completion:nil];
   
}
- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setTitle:@"删除好友" forState:UIControlStateNormal];
        [_deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _deleteButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _deleteButton.layer.cornerRadius = 20;
        _deleteButton.layer.masksToBounds = YES;
        _deleteButton.backgroundColor = [UIColor redColor];
        [_deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
    }
    
    return _deleteButton;
}
-(UIView *)footerView{
    if (_footerView == nil) {
        
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 190)];
        _footerView.backgroundColor = [UIColor whiteColor];
        
    }
    
    return _footerView;
}
-(UILabel *)nameLable{
    
    if (_nameLable == nil) {
        
        _nameLable = [[UILabel alloc]init];
        _nameLable.font = [UIFont systemFontOfSize:15];
        _nameLable.textColor = [UIColor blackColor];
        _nameLable.textAlignment = NSTextAlignmentCenter;
    }
    
    return _nameLable;
}
-(UILabel *)nickNameLable{
    
    if (_nickNameLable == nil) {
        
        _nickNameLable = [[UILabel alloc]init];
//        _nickNameLable.text = @"前去添加备注";
        _nickNameLable.font = [UIFont systemFontOfSize:15];
        _nickNameLable.textColor = [UIColor blackColor];
        _nickNameLable.textAlignment = NSTextAlignmentRight;
    }
    
    return _nickNameLable;
}
-(UIView *)headerView{
    if (_headerView == nil) {
        
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
        _headerView.backgroundColor = [UIColor whiteColor];
        
    }
    
    return _headerView;
}
-(UIImageView *)headImageView{
    
    if (_headImageView == nil) {
        
        _headImageView = [[UIImageView alloc]init];
        _headImageView.backgroundColor = [UIColor clearColor];
        _headImageView.layer.cornerRadius = 27.5;
        _headImageView.clipsToBounds = YES;
    }
    
    return _headImageView;
}


- (UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = self.footerView;
//        _tableView.tableFooterView = [[UIView alloc]init];;
        _tableView.tableHeaderView = self.headerView;
        _tableView.estimatedRowHeight           = 44;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = NO;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
        }
       
    }
    return _tableView;
}
-(UISwitch *)skiTopSwithBtn{

    if (_skiTopSwithBtn == nil) {

        _skiTopSwithBtn = [[UISwitch alloc]init];
        _skiTopSwithBtn.transform = CGAffineTransformMakeScale(0.75, 0.75);
        _skiTopSwithBtn.onTintColor = [UIColor colorWithRed:88/255.0 green:142/255.0 blue:247/255.0 alpha:1];
        [_skiTopSwithBtn addTarget:self action:@selector(skipOnAction) forControlEvents:UIControlEventTouchUpInside];

    }

    return _skiTopSwithBtn;
}
-(UISwitch *)distubSwithBtn{

    if (_distubSwithBtn == nil) {

        _distubSwithBtn = [[UISwitch alloc]init];
        _distubSwithBtn.transform = CGAffineTransformMakeScale(0.75, 0.75);
        _distubSwithBtn.onTintColor = [UIColor colorWithRed:88/255.0 green:142/255.0 blue:247/255.0 alpha:1];
        [_distubSwithBtn addTarget:self action:@selector(disturbAction) forControlEvents:UIControlEventTouchUpInside];

    }

    return _distubSwithBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
